// pages/activity/turntable/my/my.js

let utilApp = require('../../../../utils/app');
const {
  regeneratorRuntime
} = global



// 领取成功函数
function getPrizeSuccess() {
  wx.showToast({
    title: '领取成功',
  })
} 


Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    status:2,
    luckList: [

    ],
    hiddenModal:false,
    phoneNum: '',
    twoPhoneNum: ''
  },
  listenerConfirm: async function () {
    let that=this;
    let userInfo = wx.getStorageSync('userInfo');
    var myreg = /^[1][0-9][0-9]{9}$/;
    if (!myreg.test(this.data.phoneNum)) {
      utilApp.showModal('提示', '请输入正确的手机号码', '确认', function () { }, function () { }, false)
    } else if (this.data.phoneNum != this.data.twoPhoneNum) {
      utilApp.showModal('提示', '两次号码不一致', '确认', function () { }, function () { }, false)
    } else {
      let getInviter = await utilApp.lotteryDoPost('User/ajax_bind', {
        openId: userInfo.openId,
        phone: that.data.phoneNum
      });
      if (getInviter.data.status == 1) {
        that.setData({
          hiddenModal: false,
          status: 1
        })

        utilApp.showModal('提示', '请前往公众号“猫姐帮你选”,回复数字: 55领取奖品', '复制兑换', function () {
          wx.setClipboardData({
            data: '55',
            success: function (res) {
              wx.getClipboardData({
                success: function (res) {
                  
                  wx.showToast({
                    title: '复制成功'
                  })
                }
              })
            }
          })
        }, function () { }, false)
      } else {
        utilApp.showModal('提示', getInviter.data.msg, '确定', function () { }, function () { }, false)
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function() {
    let that = this;
    let wxGetSetting = utilApp.promisify(wx.getSetting);
    let isUserInfo = await wxGetSetting()
    let userInfo = wx.getStorageSync('userInfo');
    if (!isUserInfo.authSetting['scope.userInfo'] || !userInfo) {
      utilApp.isGetUserInfo('../../../authorization/authorization', '../activity/turntable/index/index')
    } else {
      let msg = await utilApp.lotteryDoPost('User/getUserInfo', {
        openId: userInfo.openId
      });
      console.log(msg)
      if (msg.data.userinfo.lottery_phone==0){
        that.setData({
          status:0
        })
      }else{
        that.setData({
          status: 1
        })
      }
      if (msg.data.userinfo.is_lottery==0){
        that.setData({
          show:false
        })
      }else{
        that.setData({
          show: true
        })
      }
    }
  },
  // 记录中奖
  async getPrizeluck(openId, winning_id,index,that){
    let getPrize = await utilApp.lotteryDoPost('Lottery/getPrize', {
      openId: openId,
      winning_id: winning_id
    });
    if (getPrize.data.status == 1) {
      getPrizeSuccess()
      let newLuckList = that.data.luckList;
      newLuckList[e.currentTarget.dataset.index].status = 1
      that.setData({
        luckList: newLuckList
      })
    } else {
      utilApp.showErr(getPrize.data.msg, 2000)
    }
  },
  // 领取奖品
  getPrize: function(e) {
    let that = this;
    
    utilApp.showModal('恭喜您', '获得5元话费+ofo骑行券+喜马拉雅券，快来领取奖品吧', '立即领取', function () {
      that.setData({
        hiddenModal: true
      })
    }, function () { }, true) 
  },
  share() {

  },
  /** 
   *  封装函数开始
   */
  // 储存登录信息
  async setLoginMsg() {
    let json = {};
    let userInfo = wx.getStorageSync('userInfo');
    const wxLogin = utilApp.promisify(wx.login);
    let {
      code
    } = await wxLogin();
    let openId = await utilApp.lotteryDoPost('login/getSessionkey', {
      code
    });
    json.openid = openId.data.openid;
    let authLogin = await utilApp.lotteryDoPost('login/authLogin', {
      openId: openId.data.openid,
      nickName: userInfo.nickName,
      headUrl: userInfo.avatarUrl
    });
    console.log(authLogin)
    json.uid = authLogin.data.arr.uid;
    wx.setStorageSync('lotteryLoginMsg', json);
    return json;
  },
  getPrizee(){
    utilApp.showModal('提示', '请前往公众号“猫姐帮你选”,回复数字: 55领取奖品', '复制兑换', function () {
      wx.setClipboardData({
        data: '55',
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {

              wx.showToast({
                title: '复制成功'
              })
            }
          })
        }
      })
    }, function () { }, false)
  },
  async getMyMsg(openid, that) {
    let myprize = await utilApp.lotteryDoPost('User/myprize', {
      openId: openid
    });
    if (myprize.data.my_winning.length != 0) {
      that.setData({
        show: true
      })
    }
    let prizeName = ['一等奖', '二等奖', '三等奖', '三等奖', '四等奖', '四等奖', '五等奖','五等奖']
    if (myprize.data.status == 1) {
      for (let i = myprize.data.my_winning.length - 1; i >= 0; i--){
        if (myprize.data.my_winning[i].end_time <= Date.parse(new Date()) / 1000) {
          myprize.data.my_winning[i].status = '2'
        }
        myprize.data.my_winning[i].getEnd_time = utilApp.getTime(myprize.data.my_winning[i].end_time);
        if (myprize.data.my_winning[i].prize_id == 3 || myprize.data.my_winning[i].prize_id == 5 || myprize.data.my_winning[i].prize_id == 8) {
          myprize.data.my_winning.splice(i, 1)
        }
      }
      myprize.data.my_winning.map(item=>{
        item.prize_id = prizeName[item.prize_id - 1]
      })
      that.setData({
        luckList: myprize.data.my_winning.reverse()
      })
    } else {
      utilApp.showErr('获取数据失败', 2000)
    }
  },
  phoneNum(e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },
  twoPhoneNum(e) {
    this.setData({
      twoPhoneNum: e.detail.value
    })
  },

  /** 
   *  封装函数结束
   */


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


})